package com.happy.qademo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.boot.CommandLineRunner;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class HappyApiApplication extends SpringBootServletInitializer implements CommandLineRunner {
    static InetAddress address;
    @Autowired
    private Environment environment;

    static {
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    static String host = address != null ? address.getHostAddress() : "127.0.0.1";

    public static void main(String[] args) {
        SpringApplication.run(HappyApiApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HappyApiApplication.class);
    }

    @Override
    public void run(String... args) throws Exception {
        String start_port = environment.getProperty("server.port");
        System.out.println("请在浏览器打开：http://" + host + ":" + start_port);
    }
}
